package com.seventh.associationsystem.controller;

import com.seventh.associationsystem.bean.Announcement;
import com.seventh.associationsystem.bean.HttpResult;
import com.seventh.associationsystem.bean.ResultCode;
import com.seventh.associationsystem.service.AnnouncementService;
import com.seventh.associationsystem.service.AssociationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private AssociationService associationService;

    @PostMapping("add")
    public HttpResult addAnnouncement(@RequestParam String associationUid, @RequestParam(value = "title", required = false) String title, @RequestParam(value = "description", required = false) String description) {
        Date now = new Date();
        if (title == null)
            return new HttpResult(ResultCode.NO_DESCRIPTION, "标题不能为空！");
        if (description == null)
            description = "";
        Announcement announcement = new Announcement();
        announcement.setAnnounceTime(now);
        announcement.setEditTime(now);
        announcement.setTitle(title);
        announcement.setDescription(description);
        announcement.setAssociation(associationService.getByUid(associationUid));
        announcementService.addAnnouncement(announcement);
        return new HttpResult(ResultCode.NO_ERROR);
    }

    @PostMapping("edit")
    public HttpResult editAnnouncement(@RequestParam String announcementUid, @RequestParam(value = "title", required = false) String title, @RequestParam(value = "description", required = false) String description) {
        Date now = new Date();
        Announcement announcement = announcementService.getAnnouncement(announcementUid);
        if (title != null)
            announcement.setTitle(title);
        if (description != null)
            announcement.setDescription(description);
        announcement.setEditTime(now);
        announcementService.updateAnnouncement(announcement);
        return new HttpResult(ResultCode.NO_ERROR);
    }

//    @GetMapping("all")
//    public Page<Announcement> getAnnouncement(@RequestParam String uid,
//                                              @RequestParam(value = "page", defaultValue = "0") int page,
//                                              @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
//                                              @RequestParam(value = "pageSortField", required = false) String pageSortField,
//                                              @RequestParam(value = "pageSortDirection", defaultValue = "ASC") String pageSortDirection) {
//        return announcementService.getAnnouncementsPageable(associationService.getByUid(uid), new PageRequest(page, pageSize, getOrders(pageSortField, pageSortDirection)));
//    }

    @GetMapping("all")
    public List<Announcement> getAnnouncements(@RequestParam String uid) {
        List<Announcement> result = announcementService.getAnnouncements(associationService.getByUid(uid));
        result.sort((a, b) -> a.getEditTime().before(b.getEditTime()) ? -1 : 1);
        return result;
    }

    private static Sort getOrders(String pageSortField, String pageSortDirection) {
        if (pageSortField != null)
            if (pageSortDirection.compareToIgnoreCase("desc") == 0)
                return new Sort(Sort.Direction.DESC, pageSortField);
            else
                return new Sort(Sort.Direction.ASC, pageSortField);
        else
            return new Sort(Sort.Direction.DESC, "editTime");
    }
}
